//
//  SuggestViewController.m
//  beautyLamp
//
//  Created by Tam Kwok To Michael on 2017/8/16.
//  Copyright © 2017年 Tam Kwok To Michael. All rights reserved.
//

#import "SuggestViewController.h"
#import "BLHeader.h"
#import "SuggestNet.h"
#import <MBProgressHUD.h>

@interface SuggestViewController ()
@property (weak, nonatomic) IBOutlet UITextView *textView;

@end

@implementation SuggestViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self setupUI];
}

- (void)setupUI {
    self.edgesForExtendedLayout = UIRectEdgeNone;
    self.title = @"意见反馈";
    UIBarButtonItem *right = [[UIBarButtonItem alloc] initWithTitle:@"完成" style:UIBarButtonItemStylePlain target:self action:@selector(commit)];
    [right setTitleTextAttributes: [NSDictionary dictionaryWithObjectsAndKeys:kAppThemeColor ,NSForegroundColorAttributeName, nil] forState:UIControlStateNormal];
    self.navigationItem.rightBarButtonItem = right;

    // 利用
    UILabel *label = [[UILabel alloc] init];
    label.numberOfLines = 0;
    label.font = [UIFont systemFontOfSize:14];
    [self.textView addSubview:label];
    label.text = @"请输入您的意见或建议...";
    label.textColor = [UIColor lightGrayColor];
    [self.textView setValue:label forKey:@"placeholderLabel"];
}

- (void)commit {
    NSString *suggest = self.textView.text;
    MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:self.view animated:YES];
    hud.mode = MBProgressHUDModeIndeterminate;
    if (!suggest || [suggest length] ==0) {
        hud.mode = MBProgressHUDModeText;
        hud.label.text = @"请输入您的建议！";
        [hud hideAnimated:YES afterDelay:2];
        return;
    }
    [SuggestNet postSuggest:suggest completer:^(id resultObject, NSError *error) {
        if (error) {
            hud.mode = MBProgressHUDModeText;
            hud.label.text = [error localizedDescription];
            [hud hideAnimated:YES afterDelay:2];
            self.textView.text = @"";
        }
    }];
    
}

@end
